package com.scenic.rd_android.testview.lstview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ListView;

import com.nova.scenic.projectlibs.util.debug.MyLog;

import com.scenic.rd_android.R;

/**
 * Created by scenic on 16/5/20.
 */
public class MyCustomListView extends ListView{
    private static final String TAG = "MyCustomListView";

    public MyCustomListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mGesture = new GestureDetector(getContext(), new GestureListener());
    }
    GestureDetector mGesture;





    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if(mGesture.onTouchEvent(ev)){
            return true;
        }
        return super.dispatchTouchEvent(ev);
    }



    class GestureListener implements GestureDetector.OnGestureListener {

        @Override
        public boolean onDown(MotionEvent e) {
            return false;
        }

        @Override
        public void onShowPress(MotionEvent e) {

        }

        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            return false;
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {

            int position = pointToPosition((int)e1.getX(),(int)e1.getY());
            MyLog.i(TAG,"positoin " + position);

            if(position != -1){
                View children = getChildAt(position - getFirstVisiblePosition());

                int type = getAdapter().getItemViewType(position);
                if(type == 1){
                    com.scenic.rd_android.testview.lstview.HorizontalListView horizontalListView =
                            (com.scenic.rd_android.testview.lstview.HorizontalListView) children.findViewById(R.id.id_item_horizontal_listview);

                    horizontalListView.dispatchTouchEvent(e2);
                    return true;
                }

            }

            return false;

        }

        @Override
        public void onLongPress(MotionEvent e) {

        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            return false;
        }
    }
}
